package com.sz.app.oss.api.trade;

import com.sz.app.oss.constants.MessagesCodes;
import com.sz.app.oss.constants.Permissions;
import com.sz.biz.app.web.BaseController;
import com.sz.biz.trade.ord.entity.TrdOrderType;
import com.sz.biz.trade.goods.service.GoodsService;
import com.sz.biz.trade.goods.dto.GoodsDto;
import com.sz.biz.trade.goods.dto.GoodsInfoDto;
import com.sz.biz.trade.goods.dto.GoodsInfoShortDto;
import com.sz.common.base.dto.QueryParams;
import com.sz.common.base.dto.ResultDto;
import com.sz.common.base.utils.StringUtils;
import io.swagger.annotations.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.List;
import java.util.Map;

/**
 * Function: 商品管理 <br>
 * Author: jifeixing <br>
 * Date: 2017-07-12 14:18:00
 */
@RestController
@RequestMapping("${api}/trd/goods")
@Api(description = " ", tags = "044、贸易-商品管理")
public class GoodsController extends BaseController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 新建商品
     *
     * @throws Exception
     */
    @ApiOperation(value = "新建商品", notes = "新建商品<br>权限：" + Permissions.BIZ_MGT_BASE_TRADE)
    @RequestMapping(value = "/save", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRADE)
    public ResultDto save(@RequestBody @ApiParam(value = "商品信息", required = true) @Validated GoodsDto goodsDto) throws ParseException {
        Integer id = goodsService.save(goodsDto);
        ResultDto result = this.getSaveSuccessDto(MessagesCodes.MODULE_TRADE_GOODS);
        result.setData(id);
        return result;
    }

    /**
     * 编辑商品
     *
     * @throws Exception
     */
    @ApiOperation(value = "编辑商品", notes = "编辑商品<br>权限：" + Permissions.BIZ_MGT_BASE_TRADE)
    @RequestMapping(value = "/update", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRADE)
    public ResultDto update(@RequestBody @ApiParam(value = "商品信息", required = true) @Validated GoodsDto goodsDto) throws ParseException {
        goodsService.update(goodsDto);
        return this.getUpdateSuccessDto(MessagesCodes.MODULE_TRADE_GOODS);
    }

    /**
     * 删除商品
     * @param ids
     * @return
     */
    @ApiOperation(value = "删除商品", notes = "删除商品<br>权限：" + Permissions.BIZ_MGT_BASE_TRADE)
    @ApiImplicitParams(
            {@ApiImplicitParam(name = "ids", value = "商品id数组： [1,2]", required = true, dataType = "array", paramType = "body")})
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRADE)
    public ResultDto batchDeleteByIds(@RequestBody List<Integer> ids) {
        goodsService.batchDeleteByIds(ids);
        return getDeleteSuccessDto(MessagesCodes.MODULE_TRADE_GOODS);
    }

    /**
     * 商品启用,停用
     * @param map
     * @return
     */
    @ApiOperation(value = "商品启用,停用", notes = "商品启用<br>权限：" + Permissions.BIZ_MGT_BASE_TRADE)
    @ApiImplicitParams({
        @ApiImplicitParam(name = "map", value = "ids(商品id数组： [1,2]), \r\nisEnabled(true: 启用；false： 停用)", required = true, dataType = "Map", paramType = "body")})
    @RequestMapping(value = "/enabled", method = {RequestMethod.POST})
    @RequiresPermissions(Permissions.BIZ_MGT_BASE_TRADE)
    public ResultDto enabledByIds(@RequestBody Map<String, Object> map) {
        Boolean isEnabled = goodsService.enableByIds(map);
        if(isEnabled){
            return getOnlineSuccessDto();
        }else{
            return getOfflineSuccessDto();
        }
    }

    /**
     * 根据id查询商品
     */
    @ApiOperation(value = "根据id查询商品", notes = "根据id查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "商品id ", required = true, dataType = "Integer", paramType = "path")})
    @RequestMapping(value = "/{id}", method = {RequestMethod.GET})
    public ResultDto getGoodsById(@PathVariable Integer id) {
        GoodsInfoDto goodsDto = goodsService.findById(id);
        ResultDto dto = new ResultDto();
        dto.setData(goodsDto);
        return dto;
    }

    /**
     * 商品列表
     */
    @ApiOperation(value = "商品列表", notes = "商品列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "businessTypeId", value = "业务类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "goodsTypeId", value = "商品类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "projectLimit", value = "项目限制：0: 全部项目； 1： 指定项目", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "projectIds", value = "项目id数组,多个逗号分隔", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "purchaserLimit", value = "采购限制： 0: 全部企业； 1： 指定企业", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "purchaserCustomerCodes", value = "采购企业code数组，多个逗号分隔", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "商品名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "supplier", value = "供应商名称", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "isEnabled", value = "启用状态： true：启用； false： 已停用", dataType = "boolean", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list", method = {RequestMethod.GET})
    public ResultDto findGoodsList(@RequestParam(required = false) String q,
                                     @RequestParam(required = false) Integer businessTypeId,
                                     @RequestParam(required = false) Integer goodsTypeId,
                                     @RequestParam(required = false) Integer projectLimit,
                                     @RequestParam(required = false) String projectIds,
                                     @RequestParam(required = false) Integer purchaserLimit,
                                     @RequestParam(required = false) String purchaserCustomerCodes,
                                     @RequestParam(required = false) String name,
                                     @RequestParam(required = false) String supplier,
                                     @RequestParam(required = false) Boolean isEnabled,
                                     @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                     @RequestParam(required = false, defaultValue = "10") int pageSize,
                                     @RequestParam(required = false) String sort,
                                     @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        q = StringUtils.replaceSqlPattern(q);
        name = StringUtils.replaceSqlPattern(name);
        QueryParams params = new QueryParams(pageSize, pageIndex, q, sort, asc);
        params.put("businessTypeId", businessTypeId);
        params.put("goodsTypeId", goodsTypeId);
        params.put("projectLimit", projectLimit);
        if(!StringUtils.isEmpty(projectIds)){
            params.put("projectIds", projectIds.split(","));
        }
        params.put("purchaserLimit", purchaserLimit);
        if(!StringUtils.isEmpty(purchaserCustomerCodes)){
            params.put("purchaserCustomerCodes", purchaserCustomerCodes.split(","));
        }
        params.put("name", name);
        params.put("supplier", supplier);
        params.put("isEnabled", isEnabled);
        return goodsService.findByFilter(params);
    }

    /**
     * 商品列表,返回id,code,name
     */
    @ApiOperation(value = "商品列表,返回id,code,name", notes = "商品列表,返回id,code,name")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "q", value = "模糊查询参数", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "businessType", value = "业务类型", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "orderType", value = "订单类型", dataType = "String", paramType = "query"),
            @ApiImplicitParam(name = "goodsTypeId", value = "商品类型", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "projectId", value = "项目id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sellId", value = "销售方id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "purchaserId", value = "采购商id", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "分页", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "页面大小，max2000", required = false, dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "sort", value = "排序的字段", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "asc", value = "排序方式", dataType = "boolean", paramType = "query", defaultValue = "true")})
    @RequestMapping(value = "/list/short", method = {RequestMethod.GET})
    public ResultDto findGoodsShortList(@RequestParam(required = false) String q,
                                     @RequestParam(required = false, defaultValue = "2") Integer businessType,
                                     @RequestParam(required = false) TrdOrderType orderType,
                                     @RequestParam(required = false) Integer goodsTypeId,
                                     @RequestParam(required = false) Integer projectId,
                                     @RequestParam(required = false) Integer sellId,
                                     @RequestParam(required = false) Integer purchaserId,
                                     @RequestParam(required = false, defaultValue = "1") int pageIndex,
                                     @RequestParam(required = false, defaultValue = "10") int pageSize,
                                     @RequestParam(required = false) String sort,
                                     @RequestParam(required = false, defaultValue = "true") Boolean asc) {
        return goodsService.findGoodsShortList(q, businessType, orderType, goodsTypeId, projectId, sellId, purchaserId, pageIndex, pageSize, sort, asc);
    }


    /**
     * 商品列表,返回订单需要字段
     */
    @ApiOperation(value = "商品列表,返回订单需要字段", notes = "商品列表,返回订单需要字段")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", dataType = "int", paramType = "path")})
    @RequestMapping(value = "/{goodsId}/order", method = {RequestMethod.GET})
    public ResultDto findGoodsShortListForOrder(@PathVariable Integer goodsId) {
        GoodsInfoShortDto goodsDto = goodsService.findShortById(goodsId);
        ResultDto dto = new ResultDto();
        dto.setData(goodsDto);
        return dto;
    }
}
